import requests
import time
import config

class Sms(object):

    def __init__(self):
        self.username = config.xunyaosoft_username
        self.pwd = config.xunyaosoft_pwd
        self.request_obj = requests.session()

    def login(self):
        url = "http://api.xunyaosoft.com/zc/zhicode/api.php?code=signIn&uPhoneNo={username}&uPassword={pwd}".format(
            username=self.username,
            pwd=self.pwd
        )
        ret = self.request_obj.get(url=url, timeout=5)
        print(ret.text)
        time.sleep(6)

    def get_phonenum(self, name="小红书"):
        url = "http://api.xunyaosoft.com/zc/zhicode/api.php?code=getPhoneNo&projName={name}".format(
            name=name
        )
        ret = self.request_obj.get(url=url, timeout=5)
        print(ret.text)
        if "未登录" in ret.text:
            self.login()
        time.sleep(6)
        return ret.text

    def get_code(self, phonenumber, name="小红书"):
        url = "http://api.xunyaosoft.com/zc/zhicode/api.php?code=getMsg&projName={name}&phoneNo={phonenumber}".format(
            name=name,
            phonenumber=phonenumber
        )
        ret = self.request_obj.get(url=url, timeout=5)
        print(ret.text)
        if "未登录" in ret.text:
            self.login()
        time.sleep(6)
        return ret.text